package com.fick.struts.action;

import java.io.File;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.hibernate.dao.LinkClassDAO;
import com.fick.hibernate.model.LinkClass;
import com.fick.tools.SystemLogger;
/**
 * 链接分类管理
 * add by 王浩强
 * */
public class LinkClassAction extends DispatchAction {

	private LinkClassDAO dao=new LinkClassDAO();
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String id=request.getParameter("id");
		String className=request.getParameter("className");
		if (id!=null && id.trim().length()>0) {
			//修改操作
			LinkClass linkClass=dao.findById(Integer.parseInt(id));
			if (linkClass!=null) {
				linkClass.setClassName(className);
				dao.update(linkClass);
			}
		}else {
			//添加操作
			LinkClass linkClass=new LinkClass();
			linkClass.setClassName(className);
			dao.save(linkClass);
		}
		return mapping.findForward("linkClass");
	}

	public ActionForward delById(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String id=request.getParameter("id");
		if (id!=null && id.trim().length()>0) {
			LinkClass linkClass=dao.findById(Integer.parseInt(id));
			if (linkClass!=null) {
				dao.delete(linkClass);
			}
		}
		return mapping.findForward("linkClass");
	}
	public ActionForward delAll(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String str = request.getParameter("str");
		LinkClass link;
		String[] ids = str.split(",");
		int id = 0;
		try {
			for (int i = 0; i < ids.length; i++) {
				id = Integer.parseInt(ids[i]);
				link = dao.findById(id);
				if(link!=null){
					dao.delete(link);
				}
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			log.error(e);
		}
		return mapping.findForward("linkClass");
	}
}
